<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog
      :close-on-click-modal="false"
      title="添加奖励"
      :modal-append-to-body="false"
      :visible.sync="visible"
      :show="show"
      :before-close="closeDialog"
      @close="$emit('update:show',false)"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="data" :rules="dataYZ" ref="data" class="mar_top" label-width="100px">
        <el-form-item label="奖励名称" prop="title">
          <el-col :span="4">
            <el-input placeholder="请输入奖励名称" v-model="data.title" style="width:190px" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="奖励标识" prop="iden">
          <el-col :span="4">
            <el-input placeholder="请输入奖励标识" v-model="data.iden" style="width:190px" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="类型" prop="types">
          <el-select
            v-model="data.types"
            placeholder="请选择"
            @change="handChange"
            style="width:190px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        

        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="addSave('data')"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>


<style lang="less" scoped></style>

<script>

export default {
  data() {
    var checkTitle = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("标题不能为空"));
      } else {
        callback();
      }
    };
    var checkIden = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("标识不能为空"));
      } else {
        callback();
      }
    };
    return {
      options: [
        {
          value: "",
          label: ""
        }
      ],
      value: "",
      data: {
        iden: "",
        title: "",
        types: "0"
      },
      loading: false,
      dialogFormVisible: false,
      dataYZ: {
        title: [{ validator: checkTitle, trigger: "blur" }],
        iden: [{ validator: checkIden, trigger: "blur" }]
      },
      visible: this.show,
      
    };
  },
  
  props: {
    show: Boolean
  },
  methods: {
    closeDialog() {
      this.$refs["data"].resetFields();
      this.visible = false;
    },
    getTypesList() {
      var data = {};
      this.$api.getAdminuser.getRewardTypes(data).then(res => {
        this.options = res.data.result;
      });
    },
    handChange(val) {
      console.log(val);
      this.data.types = val;
    },
    addSave(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var params = {};
          params["iden"] = this.data.iden;
          params["types"] = this.data.types;
          params["title"] = this.data.title;
          console.log(params);
          this.$api.getAdminuser.rewardSave(params).then(res => {
            console.log(res);
            if (res.data.code == 200 && res.data.result != null) {
              this.$emit("parent", "这里刷新父页面");
              this.$fnc.alertSuccess({ text: "添加成功" }); //成功弹出框

              this.visible = false;
            } else {
              this.$fnc.alertError(res.data.result); //失败弹出框
            }
          });
        }
      });
    }
  },
  watch: {
    show() {
      this.visible = this.show;
    }
  },
  mounted() {
    this.getTypesList();
  },
  created() {}
};
</script>
